package mju.cxf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mju.cxf.dto.AdminDTO;
import mju.cxf.dto.AdminLoginDTO;
import mju.cxf.dto.ResetPasswordDTO;
import mju.cxf.entity.Admin;
import mju.cxf.constant.MessageConstant;
import mju.cxf.exception.LoginFailedException;
import mju.cxf.mapper.AdminMapper;
import mju.cxf.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mju.cxf.utils.SHA256Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author 乐昌新
 * @since 2024-09-26
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    private final AdminMapper adminMapper;
    @Autowired
    public AdminServiceImpl(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    /**
     * A管理员登陆
     * @param adminLoginDTO
     * @return
     */
    @Override
    public Admin loginAdmin(AdminLoginDTO adminLoginDTO) {
        Admin admin =new Admin();
        BeanUtils.copyProperties(adminLoginDTO,admin);
        LambdaQueryWrapper<Admin> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Admin::getUsername,admin.getUsername());
        Admin adminByLogin = baseMapper.selectOne(lqw);
        if (adminByLogin != null){
            //密码加密
            String encrypt = SHA256Util.encrypt(admin.getPassword());
            //判断密码校验是否成功
            if (encrypt.equals(adminByLogin.getPassword())){
                return adminByLogin;
            }else{
                throw new LoginFailedException(MessageConstant.PASSWORD_ERROR);
            }
        }else{
            throw new LoginFailedException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
    }

    /**
     * A获取所有管理员信息
     * @return List
     */
    @Override
    public List<Admin> getAllAdmins() {
        return this.list();
    }

    /**
     * Aid获取管理员信息
     * @param id
     * @return
     */
    @Override
    public Admin getAdminById(int id) {
        return this.getById(id); // 使用 MyBatis-Plus 提供的 getById() 方法
    }

    /**
     * A添加管理员信息
     * @param adminDTO
     * @return
     */
    @Override
    public boolean saveAdmin(AdminDTO adminDTO) {
        Admin admin=new Admin();
        BeanUtils.copyProperties(adminDTO,admin);
        return this.save(admin); // 使用 MyBatis-Plus 提供的 save() 方法
    }

    /**
     * A修改管理员信息
     * @param adminDTO
     * @return
     */
    @Override
    public boolean updateAdmin(AdminDTO adminDTO) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminDTO,admin);
        return this.updateById(admin);
    }

    /**
     * A删除管理员信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteAdmin(Integer id) {
        return this.removeById(id);
    }

    /**
     * A分页查询管理员信息
     * @param username
     * @return
     */
    @Override
    public Page<Admin> selectPageByUsername(String username, int currentPage, int pageSize) {
        Page<Admin> page = new Page<>(currentPage, pageSize);
        return this.baseMapper.selectPageVo(page,username);
    }

    /**
     * A批量删除管理员信息
     * @param ids
     * @return
     */
    @Override
    public boolean deleteAdmins(List<Integer> ids) {
        return this.removeByIds(ids);
    }

    /**
     * A修改管理员密码
     * @param resetPasswordDTO
     * @return
     */
    @Override
    public boolean updatePassword(ResetPasswordDTO resetPasswordDTO) {
        LambdaQueryWrapper<Admin> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Admin::getId, resetPasswordDTO.getId());
        Admin admin = baseMapper.selectOne(lqw);

        //密码加密
        String encrypt = SHA256Util.encrypt(resetPasswordDTO.getPassword());
        String newEncrypt = SHA256Util.encrypt(resetPasswordDTO.getNewPassword());

        if(encrypt.equals(admin.getPassword())){
            admin.setPassword(newEncrypt);
            this.updateById(admin);
            return true;
        }
        return false;
    }
}
